<?php

/**
 * Class for getting the connection to the BD
 * @author vic
 *
 */
class DB {
	/**
	 * Gets a the mysql connection from the ini file
	 * @return mysql connection
	 */
	public static function mysqlConnection(){
		$iniFile = parse_ini_file("db.ini", true);
		if($iniFile){
				
			$host = $_SERVER['SERVER_NAME'];

			if ($iniFile[$host]) {
				$dbName = $iniFile[$host]['dbName'];
				$dbPort = $iniFile[$host]['dbPort'];
				$dbUser = $iniFile[$host]['dbUser'];
				$dbPass = $iniFile[$host]['dbPass'];
			} else {
				throw new Exception("DB host not found in DB.ini file");
			}

			if (!$dbCon = mysql_connect($host, $dbUser, $dbPass)) {
				throw new Exception("Could not connect to mysql on $host");
			}

			if (!mysql_select_db($dbName, $dbCon)) {
				throw new Exception("Could not use database $dbName");
			}
			return $dbCon;
		}
		else{
			throw new Exception(".ini file not found");
		}
	}

	/**
	 * Gets a PDO connection from the ini file
	 * @return unknown_type
	 */
	public static function PDOConnection(){
		$iniFile = parse_ini_file("../../db.ini", true);
		if($iniFile){

			$server = $_SERVER['SERVER_NAME'];

			if ($server) {
				$host = $iniFile[$server]['host'];
				$dbName = $iniFile[$server]['dbName'];
				$dbPort = $iniFile[$server]['dbPort'];
				$dbUser = $iniFile[$server]['dbUser'];
				$dbPass = $iniFile[$server]['dbPass'];
			} else {
				throw new Exception("DB host not found in DB.ini file");
			}
			try{
				$dbInfo = "mysql:host=$host;port=$dbPort;dbname=$dbName";
				//echo $dbInfo, $dbUser, $dbPass;
				return new PDO($dbInfo, $dbUser, $dbPass, array(PDO::FETCH_ASSOC));
			} catch(PDOException $e){
				throw $e;
			}
		}
		else{
			throw new Exception(".ini file not found");
		}
	}
}
?>